1494C - 1D Sokoban - CodeForces Solution


binary search dp greedy implementation two pointers *1900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
int calc(const vector<int> &a, const vector<int> &b){
    int n = a.size();
    int m = b.size();
    vector<int> su(n + 1);
    int r = m - 1;
    for (int i = n - 1; i >= 0; --i){
        su[i] = su[i + 1];
        while (r >= 0 && b[r] > a[i]) --r;
        if (r >= 0 && b[r] == a[i]) ++su[i];
    }
    int ans = 0;
    int j = 0;
    r = 0;
    for(int l=0;l< m;l++){
        while (j < n && a[j] <= b[l] + j) ++j;
        while (r < m && b[r] - b[l] < j) ++r;
        ans = max(ans, r - l + su[j]);
    }
    return ans;
}

int main() {
    int t;
    cin>>t;
    while( t--){
        int n, m;
        cin>>n>>m;
        vector<int> a(n), b(m);
        for(int i=0;i< n;i++) cin>>a[i];
        for(int i=0;i< m;i++) cin>>b[i];
        vector<int> al, bl, ar, br;
        for(int i=0;i< n;i++){
            if (a[i] < 0) al.push_back(-a[i]);//lo ve como si estuvieran
            //a la derecha las cajas de la izquierda
            else ar.push_back(a[i]);//posición de las cajas de la derecha
        }
        for(int i=0;i< m;i++){
            if (b[i] < 0) bl.push_back(-b[i]);//lo ve como si estuvieran
            //a la derecha las posiciones especiales
            else br.push_back(b[i]);//posiciones especiales de la derecha
        }
        sort(al.begin(), al.end());//lo ordena de menor a mayor
        sort(bl.begin(), bl.end());//lo ordena de menor a mayor
        cout<<calc(al, bl) + calc(ar, br)<<"\n";
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

714B - Filya and Homework
31A - Worms Evolution
1691A - Beat The Odds
433B - Kuriyama Mirai's Stones
892A - Greed
32A - Reconnaissance
1236D - Alice and the Doll
1207B - Square Filling
1676D - X-Sum
1679A - AvtoBus
1549A - Gregor and Cryptography
918C - The Monster
4B - Before an Exam
545B - Equidistant String
1244C - The Football Season
1696B - NIT Destroys the Universe
1674A - Number Transformation
1244E - Minimizing Difference
1688A - Cirno's Perfect Bitmasks Classroom
219A - k-String
952A - Quirky Quantifiers
451B - Sort the Array
1505H - L BREAK into program
171E - MYSTERIOUS LANGUAGE
630D - Hexagons
1690D - Black and White Stripe
1688D - The Enchanted Forest
1674C - Infinite Replacement
712A - Memory and Crow
1676C - Most Similar Words